/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar.wicket;

import java.awt.Color;
import java.awt.Font;
import java.util.Properties;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;
import org.wicketstuff.calendar.markup.html.form.DateTextField;
import org.wicketstuff.calendar.util.DateConverter;

import com.servoy.j2db.server.headlessclient.dataui.StyleAttributeModifierModel;
import com.servoy.j2db.server.headlessclient.dataui.TemplateGenerator;
import com.servoy.j2db.server.headlessclient.dataui.TooltipAttributeModifier;
import com.servoy.j2db.ui.IStylePropertyChanges;
import com.servoy.j2db.util.PersistHelper;

/**
 * 
 * @author servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class WicketDateTextField extends DateTextField implements IStylePropertyChanges {

	private static final long serialVersionUID = 1L;
	
	public ExtendedChangesRecorder jsChangeRecorder = new ExtendedChangesRecorder(TemplateGenerator.DEFAULT_FIELD_BORDER_SIZE, TemplateGenerator.DEFAULT_FIELD_PADDING);
	private boolean readOnly;
	private boolean valueChanged;

	/**
	 * @param id
	 * @param model
	 * @param converter
	 */
	public WicketDateTextField(String id, IModel model, DateConverter converter) {
		super(id, model, converter);
		
		add(StyleAttributeModifierModel.INSTANCE);
		
		add(TooltipAttributeModifier.INSTANCE);

		setVersioned(false);
		setOutputMarkupId(true);

		String pattern = converter.getDatePattern();
		if (pattern != null) {
			int size = pattern.length();
			add(new SimpleAttributeModifier("size", ""+size));
		}
	}
	
	protected void onComponentTag(final ComponentTag tag) {
		super.onComponentTag(tag);
		if (isReadOnly()) {
			tag.put("readonly", "readonly");
		}
	}
	
	/**
	 * @return the readOnly
	 */
	public boolean isReadOnly() {
		return this.readOnly;
	}

	/**
	 * @param readOnly the readOnly to set
	 */
	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
		setChanged();
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#getChanges()
	 */
	public Properties getChanges() {
		return jsChangeRecorder.getChanges();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#isChanged()
	 */
	public boolean isChanged() {
		return jsChangeRecorder.isChanged();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setChanged()
	 */
	public void setChanged() {
		jsChangeRecorder.setChanged();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setChanges(java.util.Properties)
	 */
	public void setChanges(Properties paramProperties) {
		jsChangeRecorder.setChanges(paramProperties);
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setRendered()
	 */
	public void setRendered() {
		jsChangeRecorder.setRendered();
		valueChanged = false;
	}
	
	public void setFgcolor(Color c) {
		String color = PersistHelper.createColorString(c);
		jsChangeRecorder.setFgcolor(color);
		setChanged();
	}
	
	public void setHorizontalAlignment(int paramInt) {
		jsChangeRecorder.setHorizontalAlignment(paramInt);
		setChanged();
	}
		
	public void setFont(Font font) {
		if (font != null) {
			String fontString = PersistHelper.createFontString(font);
			jsChangeRecorder.setFont(fontString);
		}
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#isValueChanged()
	 */
	public boolean isValueChanged() {
		try {
			return jsChangeRecorder.isValueChanged();
		} catch (Exception ex) {}
		return valueChanged;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setValueChanged()
	 */
	public void setValueChanged() {
		try {
			jsChangeRecorder.setValueChanged();
		} catch (Exception ex) {}
		this.valueChanged = true;
	}

}
